package kz.pompei.OneGwt.ifaces.widget.gwtadapters;

import java.util.Collection;

import kz.pompei.OneGwt.cmodel.CommonUtil;
import kz.pompei.OneGwt.ifaces.util.Handler;
import kz.pompei.OneGwt.ifaces.util.HandlerKiller;
import kz.pompei.OneGwt.ifaces.widget.ListWidget;
import kz.pompei.OneGwt.ifaces.widget.StateMessage;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.ListBox;

public abstract class ListBox_impl_ListWidget<Record> implements ListWidget<Record> {
  
  private final ListBox widget;
  
  public ListBox_impl_ListWidget(ListBox widget) {
    this.widget = widget;
  }
  
  protected abstract String extractId(Record record);
  
  protected abstract String extractDisplayStr(Record record);
  
  @Override
  public void addOrUpdate(Record record) {
    String id = extractId( record );
    
    if (id == null) return;
    
    for (int i = 0, C = widget.getItemCount(); i < C; i++) {
      if (id.equals( widget.getValue( i ) )) {
        widget.setItemText( i, extractDisplayStr( record ) );
        return;
      }
    }
    
    widget.addItem( extractDisplayStr( record ), id );
  }
  
  @Override
  public void clean() {
    widget.clear();
  }
  
  @Override
  public void removeById(String id) {
    if (CommonUtil.isEmpty( id )) return;
    for (int i = 0, C = widget.getItemCount(); i < C; i++) {
      if (id.equals( widget.getValue( i ) )) {
        widget.removeItem( i );
      }
    }
  }
  
  @Override
  public void set(Collection<Record> records) {
    clean();
    for (Record record : records) {
      widget.addItem( extractDisplayStr( record ), extractId( record ) );
    }
  }
  
  protected String whatStateMessageText(StateMessage stateMessage) {
    if (stateMessage == null) return null;
    switch (stateMessage) {
    case EMPTY:
      return "Данных нет";
    case NO_MESSAGE:
      return null;
    case WAIT:
      return "Ждите...";
    }
    return whatUserMessageText( stateMessage );
  }
  
  protected String whatUserMessageText(StateMessage stateMessage) {
    return stateMessage.name();
  }
  
  @Override
  public void setStateMessage(StateMessage topMessage) {
    String message = whatStateMessageText( topMessage );
    if (message == null) message = "";
    widget.clear();
    widget.addItem( message, "" );
  }
  
  @Override
  public void select(String id) {
    if (id == null) {
      widget.setSelectedIndex( -1 );
      return;
    }
    for (int i = 0, C = widget.getItemCount(); i < C; i++) {
      if (id.equals( widget.getValue( i ) )) {
        widget.setSelectedIndex( i );
        return;
      }
    }
    widget.setSelectedIndex( -1 );
  }
  
  @Override
  public String getSelectedId() {
    int index = widget.getSelectedIndex();
    if (index < 0) return null;
    return widget.getValue( index );
  }
  
  @Override
  public HandlerKiller addChangeSelectionHandler(final Handler handler) {
    final HandlerRegistration hr = widget.addChangeHandler( new ChangeHandler() {
      @Override
      public void onChange(ChangeEvent event) {
        handler.handle();
      }
    } );
    
    return new HandlerKiller() {
      @Override
      public void killHandler() {
        hr.removeHandler();
      }
    };
  }
  
  @Override
  public void scrollToView(String id) {
    select( id );
  }
  
  @Override
  public void setEnabled(boolean enabled) {
    widget.setEnabled( enabled );
  }
  
  @Override
  public void setVisible(boolean visible) {
    widget.setVisible( visible );
  }
  
  @Override
  public void setFocus(boolean focused) {
    widget.setFocus( focused );
  }
}
